//
//  RTVoiceSwitch.m
//  StoryToy
//
//  Created by baxiang on 2018/1/24.
//  Copyright © 2018年 roobo. All rights reserved.
//

#import "RTVoiceSwitch.h"
@interface RTVoiceSwitch ()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIView *onContentView;
@property (nonatomic, strong) UIView *offContentView;
@property (nonatomic, strong) UIImageView *knobView;
@property (nonatomic, strong) UILabel *onLabel;
@property (nonatomic, strong) UILabel *offLabel;
@property (nonatomic, strong) UIImageView *knobFrontView;
@end
@implementation RTVoiceSwitch

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView *onContentView = [[UIView alloc] initWithFrame:CGRectMake(0, (frame.size.height-25)/2, frame.size.width, 25)];
        [self addSubview:onContentView];
        onContentView.layer.cornerRadius = 25/2;
        onContentView.layer.masksToBounds = YES;
        onContentView.backgroundColor = [UIColor whiteColor];
        _onContentView = onContentView;
      
        
        UILabel *onLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, onContentView.bounds.size.width-20, 25)];
        onLabel.textAlignment = NSTextAlignmentLeft;
        onLabel.font = [UIFont systemFontOfSize:10];
        onLabel.text = @"关闭对讲";
        [onContentView addSubview:onLabel];
        _onLabel = onLabel;
    
        
        UIImageView *imageView = [UIImageView new];
        imageView.frame = CGRectMake(frame.size.width-35, 5, 35, 35);
        imageView.backgroundColor = [UIColor whiteColor];
        imageView.layer.cornerRadius = 35/2;
        imageView.userInteractionEnabled = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.layer.shadowOffset = CGSizeMake(0.5, 0.5);
        imageView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        [self addSubview:imageView];
        _knobFrontView = imageView;
        [imageView setNeedsLayout];
        
        UIImageView *knobView = [[UIImageView alloc] initWithFrame:CGRectMake(7.5, 7.5, 20, 20)];
        knobView.image = [[UIImage imageNamed:@"icon_mike"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        knobView.contentMode = UIViewContentModeScaleAspectFit;
        knobView.tintColor = [UIColor redColor];
        knobView.userInteractionEnabled = NO;
        [imageView addSubview:knobView];
        _knobView = knobView;
        
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                     action:@selector(handleTapTapGestureRecognizerEvent:)];
        [self addGestureRecognizer:tapGesture];
        _on = YES;
       
    }
    return self;
}

- (void)handleTapTapGestureRecognizerEvent:(UITapGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        [self setOn:!self.isOn animated:YES];
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

- (void)setOn:(BOOL)on animated:(BOOL)animated{
    _on = on;
    if (self.isOn) {
        CABasicAnimation *animation5 = [CABasicAnimation animationWithKeyPath:@"position"];
        [animation5 setFromValue:[NSValue valueWithCGPoint:CGPointMake(10, 20)]];
        [animation5 setToValue:[NSValue valueWithCGPoint:CGPointMake(self.frame.size.width-10, 20)]];
        [animation5 setDuration:0.3];
        animation5.removedOnCompletion = NO;
        animation5.fillMode = kCAFillModeForwards;
        [animation5 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [self.knobFrontView.layer addAnimation:animation5 forKey:NULL];
        self.knobView.image = [[UIImage imageNamed:@"icon_conneting_12"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        _onLabel.frame = CGRectMake(8, 0, self.bounds.size.width-20, 25);
        
         _onLabel.textAlignment = NSTextAlignmentLeft;
         _onLabel.text = @"关闭对讲";        
        _onContentView.backgroundColor = UIColorHex(0x7290ff);
    }else{
        CABasicAnimation *animation6 = [CABasicAnimation animationWithKeyPath:@"position"];
        [animation6 setFromValue:[NSValue valueWithCGPoint:CGPointMake(self.frame.size.width-10, 20)]];
        [animation6 setToValue:[NSValue valueWithCGPoint:CGPointMake(10, 20)]];
        [animation6 setDuration:0.3];
        animation6.removedOnCompletion = NO;
        animation6.fillMode = kCAFillModeForwards;
        [animation6 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [self.knobFrontView.layer addAnimation:animation6 forKey:NULL];
        self.knobView.image = [[UIImage imageNamed:@"icon_mike"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
         _onLabel.frame = CGRectMake(20, 0, self.bounds.size.width-30, 25);
        _onLabel.textAlignment = NSTextAlignmentRight;
        _onLabel.text = @"开启对讲";
        _onContentView.backgroundColor = UIColorHex(0xb9b9b9);
    }
}

@end
